package it.eng.get.async;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.core.task.TaskExecutor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class BeanOperationController implements Controller {

	

	private int instanceVar = 0;
	
	  private class MessagePrinterTask implements Runnable {

		    private String message;

		    public MessagePrinterTask(String message) {
		      this.message = message;
		    }

		    public void run() {
		      System.out.println(message+"--->"+(instanceVar++));
				try {
					Thread.sleep(10000);
				} catch (Exception e) {
					e.printStackTrace();
					// TODO: handle exception
				}
				System.out.println("done" + message);
		    }

		  }
	
	private TaskExecutor taskExecutor;


	public BeanOperationController(TaskExecutor taskExecutor) {
		this.taskExecutor = taskExecutor;
	}
	
	
	@Override
	public ModelAndView handleRequest(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
		// TODO Auto-generated method stub
		for(int i = 0; i < 5; i++) {
			taskExecutor.execute(new MessagePrinterTask("Message" + i));
		}
		return new ModelAndView("jPEHelp");
	}
	
}
